<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>虚拟人聊天页面</title>
    <style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }
        .container {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            width: 80%;
            background-color: white;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            margin-top: 20px;
        }
        .rectangle {
            border: 1px solid #a0c0c0;
            background-color: #c0e0e0;
            flex: 1;
            margin: 10px;
            padding: 10px;
            box-sizing: border-box;
            height: 400px;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 24px;
            color: #333;
        }
        .rectangle.large {
            flex: 3;
        }
        .chat-input {
            display: flex;
            justify-content: space-between;
            width: 100%;
            margin-top: 10px;
        }
        .chat-input input {
            flex: 1;
            margin-right: 10px;
            padding: 10px;
            font-size: 16px;
        }
        .chat-input button {
            padding: 10px 20px;
            font-size: 16px;
        }
    </style>
</head>
<body>
    <button id="runButton">运行EXE文件</button>
    <div id="result"></div>

    <div class="container">
        <div class="rectangle large">
            <video id="video-stream" controls autoplay></video>
        </div>
        <div class="rectangle">
            <div id="chat-content" style="height: 350px; overflow-y: auto; margin-bottom: 10px;">
                <!-- 聊天内容将在这里动态显示 -->
            </div>
            <div class="chat-input">
                <input type="text" id="chat-input" placeholder="在此输入消息">
                <button id="sendButton">发送</button>
            </div>
        </div>
    </div>

    <script>
        // 运行EXE文件
        document.getElementById('runButton').addEventListener('click', function() {
            fetch('http://localhost:5000/run-exe')
                .then(response => response.json())
                .then(data => {
                    document.getElementById('result').innerText = JSON.stringify(data);
                })
                .catch(error => {
                    document.getElementById('result').innerText = 'Error: ' + error;
                });
        });

        // 连接视频流
        const video = document.getElementById('video-stream');
        video.src = "http://localhost:8080/video-stream-url"; // 替换为后端的流地址

        // 聊天功能
        const chatInput = document.getElementById('chat-input');
        const sendButton = document.getElementById('sendButton');
        const chatContent = document.getElementById('chat-content');

        sendButton.addEventListener('click', function() {
            const message = chatInput.value.trim();
            if (message) {
                // 将消息发送到后端（这里假设后端提供了一个接收消息的接口）
                fetch('http://localhost:5000/send-message', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ message: message })
                })
                .then(response => response.json())
                .then(data => {
                    // 显示发送的消息
                    chatContent.innerHTML += `<p><strong>我：</strong>${message}</p>`;
                    // 显示回复的消息
                    if (data.response) {
                        chatContent.innerHTML += `<p><strong>虚拟人：</strong>${data.response}</p>`;
                    }
                    chatInput.value = ''; // 清空输入框
                    chatContent.scrollTop = chatContent.scrollHeight; // 滚动到最新消息
                })
                .catch(error => {
                    console.error('Error:', error);
                });
            }
        });

        // 按下回车键发送消息
        chatInput.addEventListener('keypress', function(event) {
            if (event.key === 'Enter') {
                sendButton.click();
            }
        });
    </script>
</body>
</html>